using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

using Wicresoft.Common;
using Wicresoft.Messages.Entity;
using Wicresoft.Framework.Organization;

namespace Wicresoft.Messages.BusinessLogic
{
    public class BLStat
    {
        public static void StatQueue(Wicresoft.Messages.Entity.Queue queue, QueueProcessStatusCode code)
        {
            try
            {
                Message message = new Message(queue.MessageID);
                message.Load();

                SendStat stat = GetExistSendStat(queue.SenderName, DateTime.Now.Date, message.ModuleID, queue.DeviceID);
                if (stat == null)
                    stat = new SendStat();

                stat.DeviceID = queue.DeviceID;
                stat.ModuleID = message.ModuleID;
                stat.StatDate = DateTime.Now.Date;
                stat.UserName = queue.SenderName;

                try
                {
                    Department department = OrganizationPublicHelper.GetMainDepartmentInfoByUser(queue.SenderName);
                    if (department != null)
                        stat.DepartmentPath = department.IDPath;
                }
                catch
                {
                }


                if (stat.SuccessCount < 0)
                    stat.SuccessCount = 0;
                if (stat.FailCount < 0)
                    stat.FailCount = 0;
                if (stat.TotalCount < 0)
                    stat.TotalCount = 0;

                stat.TotalCount++;
                if (code == QueueProcessStatusCode.Posted)
                    stat.SuccessCount++;
                else if (code == QueueProcessStatusCode.Failed || code == QueueProcessStatusCode.Unable)
                    stat.FailCount++;

                stat.Save();
            }
            catch
            {
            }
        }

        private static SendStat GetExistSendStat(string userName, DateTime statDate, Guid moduleID, int deviceID)
        {
            string whereClause = string.Format("{0} = N'{1}' AND {2} = '{3}' AND {4} = '{5}' AND {6} = {7}",
                SendStat.SQLCOL_USERNAME, userName, SendStat.SQLCOL_STATDATE, statDate, SendStat.SQLCOL_MODULEID, moduleID, SendStat.SQLCOL_DEVICEID, deviceID);
            DataTable dtStat = DataAccess.Select(typeof(SendStat), whereClause);
            if (dtStat.Rows.Count > 0)
            {
                SendStat sendStat = new SendStat();
                sendStat.Init(dtStat.Rows[0]);
                return sendStat;
            }
            return null;
        }
        

		public static void StatFeedBack(Wicresoft.Messages.Entity.FeedBack feedBack)
        {
            try
            {
				Guid moduleID = Guid.Empty;
				string senderName = feedBack.UserName;
				if (feedBack.MessageID != null && feedBack.MessageID != Guid.Empty)
				{
					Message message = new Message(feedBack.MessageID);
					message.Load();
					moduleID = message.ModuleID;
					senderName = message.SenderName;
				}
                

                ReceiveStat stat = GetExistReceiveStat(feedBack.UserName, DateTime.Now.Date, moduleID, feedBack.DeviceID);
                if (stat == null)
                    stat = new ReceiveStat();

                stat.DeviceID = feedBack.DeviceID;
                stat.ModuleID = moduleID;
                stat.StatDate = DateTime.Now.Date;
                stat.UserName = senderName;

                try
                {
                    Department department = OrganizationPublicHelper.GetMainDepartmentInfoByUser(senderName);
                    if (department != null)
                        stat.DepartmentPath = department.IDPath;
                }
                catch
                {
                }


                if (stat.TotalCount < 0)
                    stat.TotalCount = 0;

                stat.TotalCount++;
                stat.Save();
            }
            catch
            {
            }
        }

        private static ReceiveStat GetExistReceiveStat(string userName, DateTime statDate, Guid moduleID, int deviceID)
        {
            string whereClause = string.Format("{0} = N'{1}' AND {2} = '{3}' AND {4} = '{5}' AND {6} = {7}",
                ReceiveStat.SQLCOL_USERNAME, userName, ReceiveStat.SQLCOL_STATDATE, statDate, ReceiveStat.SQLCOL_MODULEID, moduleID, ReceiveStat.SQLCOL_DEVICEID, deviceID);
			if (moduleID == null || moduleID == Guid.Empty)
			whereClause = string.Format("{0} = N'{1}' AND {2} = '{3}' AND {4} IS NULL AND {6} = {7}",
                ReceiveStat.SQLCOL_USERNAME, userName, ReceiveStat.SQLCOL_STATDATE, statDate, ReceiveStat.SQLCOL_MODULEID, moduleID, ReceiveStat.SQLCOL_DEVICEID, deviceID);

            DataTable dtStat = DataAccess.Select(typeof(ReceiveStat), whereClause);
            if (dtStat.Rows.Count > 0)
            {
                ReceiveStat receiveStat = new ReceiveStat();
                receiveStat.Init(dtStat.Rows[0]);
                return receiveStat;
            }
            return null;
        }
    }
}
